<template>
  <div class="single">
    <el-row>
      <el-col :span="24">
        <el-form
          :model="formModel"
          :rules="rules"
          ref="formModel"
          label-width="110px"
          class="demo-formModel"
        >

          <!-- 任职单位名称 -->
          <el-row>
            <el-col :span="8">
              <el-form-item label="任职单位名称" prop="dwmc">
                <el-input v-model="formModel.dwmc" placeholder="请输入任职单位名称" class="el_select"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="单位性质" prop="dwxz">
                <el-select
                  v-model="formModel.dwxz"
                  placeholder="请选择单位性质"
                  class="el_select"
                >
                  <el-option
                    v-for="item in educationList"
                    :label="item.name"
                    :value="item.name"
                    :key="item.val"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="任职岗位" prop="rzgw">
                <el-input v-model="formModel.rzgw" placeholder="请输入任职岗位" class="el_select"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 任职开始年月 -->
          <el-row>
            <el-col :span="8">
              <el-form-item label="任职开始年月" prop="kssj">
                <el-date-picker
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择任职开始年月"
                  v-model="formModel.kssj"
                  style="width: 15rem"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="任职结束年月" prop="jssj">
                <el-date-picker
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择任职结束年月"
                  v-model="formModel.jssj"
                  style="width: 15rem"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row class="btns">
            <el-col :span="24">
              <el-form-item>
                <el-button type="primary" @click="submitForm('formModel')"
                >{{ btnname }}
                </el-button
                >
                <el-button @click="resetForm('formModel')">重置</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: 'worksexp',
  data () {
    return {
      cityList: [
        {name: '中国', val: 0},
        {name: '香港', val: 1},
      ],
      educationList: [
        {name: '小学', val: 0},
        {name: '大学', val: 1},
      ],
      schoolList: [
        {name: 'xxx学院', val: 0},
        {name: 'xxx学院', val: 1},
      ],
      sellist: [
        {name: '否', val: 0},
        {name: '是', val: 1},
      ],

      imageUrl: '',
      formModel: {
        jsId: '',
        dwmc: '',
        dwxz: '',
        rzgw: '',
        kssj: '',
        jssj: ''
      },
      rules: {
        dwmc: [
          {required: true, message: '请输入任职单位名称', trigger: 'blur'},
        ],
        dwxz: [
          {required: true, message: '请输入任职单位性质', trigger: 'blur'},
        ],
        rzgw: [
          {required: true, message: '请输入任职岗位', trigger: 'blur'},
        ],
        kssj: [
          {
            type: 'string', required: true, message: '请选择任职开始年月', trigger: 'change',
          },
        ],
        jssj: [
          {
            type: 'string', required: true, message: '请选择任职结束年月', trigger: 'change',
          },
        ],

      },
      btnname: '添加',
      page: {
        id: '',
        op: 'add'
      },
    }
  },
  mounted () {
    this.initdata()
  },
  methods: {
    initdata () {
      var params = this.$route.params
      console.log(params)
      this.page.id = params.id
      this.page.op = params.op
      this.get_detail()
    },
    get_detail () {
      var params = {jsId: this.page.id}
      this.$http({
        url: this.$http.adornUrl('/gzjl/getJsAllGzjl'),
        method: 'get',
        params: this.$http.adornParams(params)
      }).then(({data}) => {
        console.log(data.data)
        if (data && data.code === 0) {
          if (data.data.length > 0) {
            this.page.op = 'detail'
            this.btnname = '修改'
            this.formModel = data.data[0]
          } else {
            this.page.op = 'add'
            this.btnname = '添加'
          }
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    get_formdata (config) {
      console.log(config)
      //对象转formdata格式
      const formData = new FormData()
      //循环传入的值转换formData
      Object.keys(config).forEach((key) => {
        formData.append(key, config[key])
      })
      return formData
    },
    submitForm (formName) {
      this.formModel.jsId = this.$store.state.user.jsid
      let formdata = this.get_formdata(this.formModel)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = ''
          if (this.page.op == 'add') {
            url = '/gzjl/save'
          } else {
            url = '/gzjl/update'
          }
          this.$http({
            url: this.$http.adornUrl(url),
            method: 'post',
            data: formdata
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.$nextTick(() => {
                    if (this.page.op == 'add') {
                      this.$refs[formName].resetFields()
                    }
                  })
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })

        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
  },
}
</script>


<style>
.single {
  padding: 0.5rem;
}

.el_select {
  width: 15rem;
}

.btns {
  padding-left: 22rem;
}
</style>

